//
//  XHRLoginController.m
//  快医
//
//  Created by 张世敏 on 16/7/6.
//
//

#import "XHRLoginController.h"
#import "XHRFastRegistController.h"
#import "XHRPersonalMainPageViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "XHRNetworkingToolManager.h"
#import "XHRLoginInfo.h"
#import "JASidePanelController.h"

@interface XHRLoginController ()

/** 用户名 **/
@property (nonatomic, weak) UITextField *tf_username;
/** 证件号码 **/
@property (nonatomic, weak) UITextField *tf_password;

@end

@implementation XHRLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 初始化界面
    [self initUI];
    
}


/** 初始化界面 **/
-(void)initUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"用户登录";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"快速注册" style:UIBarButtonItemStylePlain target:self action:@selector(quicklyRegist:)];
    
    // 图片
    UIImageView *topView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"product_img_logo_nor.png"]];
    [self.view addSubview:topView];
    [topView sizeToFit];
    [topView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.offset(30);
    }];
    
    UIImageView *bottomView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"product_text_logo_nor.png"]];
    [self.view addSubview:bottomView];
    [bottomView sizeToFit];
    [bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(topView.bottom);
    }];
    
    // 输入框
    _tf_username = [self textFieldWithPlaceholder:@"请输入用户姓名" imgNamed:nil];
    _tf_username.center = CGPointMake(self.view.center.x, 200);
    
    _tf_password = [self textFieldWithPlaceholder:@"请输入密码" imgNamed:nil];
    _tf_password.center = CGPointMake(self.view.center.x, 244);
    
    // 登录按钮
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    //    [loginBtn setTitle:@"登录" forState:UIControlStateHighlighted];
    loginBtn.frame = CGRectMake(0, 0, 275, 30);
    loginBtn.center = CGPointMake(self.view.center.x, 288);
    loginBtn.layer.cornerRadius = 4;
    loginBtn.layer.masksToBounds = YES;
    
    [self.view addSubview:loginBtn];
    
    // 绑定按钮的时间
    [loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
}


/** 创建输入框 **/
-(UITextField *)textFieldWithPlaceholder:(NSString *)placeholder imgNamed:(NSString *)imgName{
    
    UITextField *textField = [[UITextField alloc] init];
    textField.placeholder = placeholder;
    textField.bounds = CGRectMake(0, 0, 275, 30);
    textField.center = CGPointMake(self.view.center.x, 100);
    
    UIView *imgSuperView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, textField.xhr_Height)];
    imgSuperView.backgroundColor = textField.backgroundColor;
    textField.leftView = imgSuperView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    
    [self.view addSubview:textField];
    
    return textField;
}


/** 快速注册 **/
-(void)quicklyRegist:(UIBarButtonItem *)sender{
    
    // 跳转到快速注册界面
    XHRFastRegistController *fastRegistVc = [[XHRFastRegistController alloc] init];
    [[XHRPersonalMainPageViewController new] pushToNewViewController:fastRegistVc];
}


/** 点击登录按钮 **/
-(void)login:(UIButton *)sender{
    
    [self.view endEditing:YES];
    
    if (_tf_username.text.length == 0 || _tf_password.text.length == 0) {
        
        return;
    }
    
    NSDictionary *parameters = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f"};
    XHRNetworkingToolManager *manager = [XHRNetworkingToolManager shareManager];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://iosapi.itcast.cn/carelinkQuickLogin.json.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 登录成功
        [XHRLoginInfo savePassword:_tf_password.text withUsername:_tf_username.text];
        
        if (self.isLoginBlock) {
            self.isLoginBlock(YES);
        }
        
        // 跳转到主界面
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"login error:%@",error);
    }];
}


/** 点击屏幕回退键盘 **/
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}

@end
